﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using MensUnoShop.Models;


namespace MensUnoShop.Helpers
{
    public static class TagHtmlMarker
    {
        /// <summary>
        /// 導覽列HtmlHelper
        /// </summary>
        public static string GetNavLinkString(this HtmlHelper helper, List<Tag> navlist)
        { 
           StringBuilder sb = new StringBuilder();

           if (navlist != null)
           {
               sb.Append("<a href=\"/\">首頁</a>");
               for (int i = 0; i < navlist.Count; i++)
               {
                   if (i == navlist.Count - 1)
                   {
                       if (navlist[i].URI == "")
                           sb.Append(" ><span>" + HttpUtility.HtmlEncode(navlist[i].TITLE) + "</span>");
                       else
                           sb.Append(" ><span><a href=\"" + navlist[i].URI + "\">" + HttpUtility.HtmlEncode(navlist[i].TITLE) + "</a></span>");
                   }
                   else
                   {
                       if (navlist[i].URI == "")
                           sb.Append(" >"+ navlist[i].TITLE );
                       else
                           sb.Append(" ><a href=\"" + navlist[i].URI + "\">" + HttpUtility.HtmlEncode(navlist[i].TITLE) + "</a>");
                   }
               }
           }
           else
               sb.Append("<span><a href=\"/\">首頁</a></span>");

           return sb.ToString();
        }
        
        //<select name="SIZE"><option>S</option><option>M</option><option>L</option></select>
        public static string GetSizeString(this HtmlHelper helper, List<Specification> splist)
        {
            StringBuilder sb = new StringBuilder();
            if (splist.Sum(s => s.STOCK) == 0)
                sb.Append("已無庫存!!");
            else
            {
                sb.Append("<select name=\"SIZE\" id=\"speccode\">");
                splist = splist.OrderBy(s => s.TITLETAGID).ToList();
                foreach (var sp in splist.Where(s => s.STOCK > 0))
                {
                    sb.Append("<option value=\"" + sp.SID + "\">" + sp.TITLE + "</option>");
                }
                sb.Append("</select>");
            }
            return sb.ToString();
        }

        //<a href="#">貨到請通知我</a>數量：已售完
        public static string GetAmountString(this HtmlHelper helper, List<Specification> splist)
        {
            StringBuilder sb = new StringBuilder();
            if (splist.Sum(s => s.STOCK) == 0)
                sb.Append("<!-- href=\"#\">貨到請通知我</a-->Quantity：已售完");
            else
            {
                sb.Append("Quantity：<select name=\"AMOUNT\" id=\"specamt\">");
                foreach (var sp in splist)
                {
                    int stock = sp.STOCK;
                    if (stock > 10)
                        stock = 10;
                    if (stock == 0)
                        sb.Append("<option class=\"sub_" + sp.SID + "\" value='0'>已無庫存</option>");
                    else
                    {
                        for (int i = 1; i <= stock; i++)
                            sb.Append("<option class=\"sub_" + sp.SID + "\" value=\"" + i + "\">" + i + "</option>");
                    }
                }
                sb.Append("</select>");
                sb.Append("<script type='text/javascript'>$(function () { makeSublist('speccode', 'specamt'); });</script>");
            }
            return sb.ToString();
        }

        public static string GetAmountOnlyString(this HtmlHelper helper, List<Specification> splist)
        {
            StringBuilder sb = new StringBuilder();
            if (splist.Sum(s => s.STOCK) == 0)
                sb.Append("<!-- href=\"#\">貨到請通知我</a-->Quantity：已售完");
            else
            {
                sb.Append("Quantity：<select name=\"AMOUNT\" id=\"specamt\">");
                foreach (var sp in splist)
                {
                    int stock = sp.STOCK;
                    if (stock > 10)
                        stock = 10;
                    if (stock == 0)
                        sb.Append("<option value='0'>已無庫存</option>");
                    else
                    {
                        for (int i = 1; i <= stock; i++)
                            sb.Append("<option value=\"" + i + "\">" + i + "</option>");
                    }
                }
                sb.Append("</select>");
            }
            return sb.ToString();
        }

        
    }
}